package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Tag(name = "文件管理")
@RequestMapping("/admin/file")
@RestController
@Slf4j
public class FileUploadController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Operation(summary = "上传文件")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();//获取原始文件名
//            String extension = fileName.substring(fileName.lastIndexOf('.'));//截取最后的扩展名
//            String objectName = UUID.randomUUID().toString()+extension;//构造新文件名称
            String path = aliOssUtil.upload(file.getBytes(), fileName);
            return Result.ok(path);
        } catch (IOException e) {
            log.error("文件上传失败：{}",e);
        }
        return Result.fail();
    }

}
